package com.woke.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jakewharton.rxbinding2.view.RxView;
import com.netease.nim.uikit.GlideApp;
import com.woke.data.UnionPayChannel;
import com.zhongcai.online.R;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;

/**
 * 作者 ： 暗网
 * 创建时间：2017/5/8
 * 备注： 入驻列表
 */

public class InTheChannelListAdapter extends RecyclerView.Adapter<InTheChannelListAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private Context mContext;
    private List<UnionPayChannel> mChannels;
    private String[] states;
    private OnItemClickListener mItemClickListener;

    public InTheChannelListAdapter(Context context, List<UnionPayChannel> list) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mChannels = list;
        this.states = context.getResources().getStringArray(R.array.in_the_list_array);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.adapter_in_the_channel_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final UnionPayChannel channel = mChannels.get(position);
        Log.e("onBindVnPayChannel",channel.toString());
        if (channel != null) {
            String imgPath = channel.getIcon();
            holder.mTitle.setText(channel.getTitle());
            if (!TextUtils.isEmpty(imgPath)) {
                if (!imgPath.startsWith("http")) {
                    imgPath = "http://" + imgPath;
                }
                GlideApp.with(mContext)
                        .load(imgPath)
                        .placeholder(R.mipmap.icn__launch_oem)
                        .error(R.mipmap.icn__launch_oem)
                        .into(holder.mIcon);
            }

            if (channel.getSettled() == 0) {
                holder.mStateTv.setText(states[0]);
            } else if (channel.getSettled() == 1) {
                holder.mStateTv.setText(states[1]);
            } else if (channel.getSettled() == 2) {
                holder.mStateTv.setText(states[2]);
            }

            RxView.clicks(holder.itemView).throttleFirst(2, TimeUnit.SECONDS).subscribe(new Consumer<Object>() {
                @Override
                public void accept(Object o) throws Exception {

                    if (mItemClickListener != null) {
                        mItemClickListener.OnItemClick(holder.itemView, channel, holder.getAdapterPosition());
                    }
                }
            });

        }

    }

    @Override
    public int getItemCount() {
        return mChannels == null ? 0 : mChannels.size();
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView mIcon;
        private TextView mTitle;
        private TextView mStateTv;


        public ViewHolder(View itemView) {
            super(itemView);
            mIcon = (ImageView) itemView.findViewById(R.id.channel_img);
            mTitle = (TextView) itemView.findViewById(R.id.channel_title);
            mStateTv = (TextView) itemView.findViewById(R.id.channel_state_tv);

        }
    }

}
